﻿using AhpilyServer;
using AhpilyServer.Concurrent;
using PokerServer.Model;
using System;
using System.Collections.Generic;

namespace PokerServer.Cache
{
    /// <summary>
    /// 角色数据缓存层
    /// </summary>
    public class UserCache
    {
        private readonly Dictionary<int, UserModel> idModelDict = new Dictionary<int, UserModel>();

        /// <summary>
        /// 账号id 对应 角色id
        /// </summary>
        private readonly Dictionary<int, int> accIdUIdDict = new Dictionary<int, int>();


        private readonly ConcurrentInt id = new ConcurrentInt(-1);

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="name"></param>
        /// <param name="accountId"></param>
        public void Create(string name, int accountId)
        {
            UserModel model = new UserModel(id.Add_Get(), name, accountId);

            // 保存到字典
            idModelDict.Add(model.Id, model);
            accIdUIdDict.Add(model.AccoundId, model.Id);
        }

        /// <summary>
        /// 跟新角色数据
        /// </summary>
        /// <param name="model"></param>
        public void Update(UserModel model)
        {
            idModelDict[model.Id] = model;
        }

        /// <summary>
        /// 判断此账号下是否有角色
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public bool IsExit(int accountId)
        {
            return accIdUIdDict.ContainsKey(accountId);
        }

        /// <summary>
        /// 查 根据账号Id获取角色信息
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public UserModel GetModelByAccountId(int accountId)
        {
            int userId = accIdUIdDict[accountId];
            UserModel model = idModelDict[userId];
            return model;
        }

        /// <summary>
        /// 根据账号id获取角色Id
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public int GetId(int accountId)
        {
            return accIdUIdDict[accountId];
        }

        /// <summary>
        ///  存储 在线玩家 只有在线玩家 才有 这个（ClientPeer)对象
        /// </summary>
        private readonly Dictionary<int, ClientPeer> idClientDict = new Dictionary<int, ClientPeer>();

        private readonly Dictionary<ClientPeer, int> clientIdDict = new Dictionary<ClientPeer, int>();

        /// <summary>
        /// 是否在线
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public bool IsOnline(ClientPeer client)
        {
            return clientIdDict.ContainsKey(client);
        }

        /// <summary>
        /// 是否在线
        /// </summary>
        /// <param name="uId"></param>
        /// <returns></returns>
        public bool IsOnline(int uId)
        {
            return idClientDict.ContainsKey(uId);
        }

        /// <summary>
        /// 角色上线
        /// </summary>
        /// <param name="client"></param>
        /// <param name="uId"></param>
        public void Online(ClientPeer client, int uId)
        {
            idClientDict.Add(uId, client);
            clientIdDict.Add(client, uId);
        }

        /// <summary>
        /// 角色下线
        /// </summary>
        /// <param name="client"></param>
        public void Offline(ClientPeer client)
        {
            int uId = clientIdDict[client];
            clientIdDict.Remove(client);
            idClientDict.Remove(uId);
        }

        /// <summary>
        /// 根据连接对象获取角色模型
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public UserModel GetModelByClientPeer(ClientPeer client)
        {
            int uId = clientIdDict[client];
            UserModel model = idModelDict[uId];
            return model;
        }

        /// <summary>
        /// 根据用户id获取数据模型
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public UserModel GetModelById(ClientPeer client)
        {
            int userId = clientIdDict[client];
            UserModel userModel = idModelDict[userId];
            return userModel;
        }

        /// <summary>
        /// 根据角色id获取连接对象
        /// </summary>
        /// <param name="uId"></param>
        /// <returns></returns>
        public ClientPeer GetClientPeer(int uId)
        {
            return idClientDict[uId];
        }

        /// <summary>
        /// 根据连接对象获取角色id
        /// </summary>
        /// <param name="client"></param>
        /// <returns></returns>
        public int GetId(ClientPeer client)
        {
            if (!clientIdDict.ContainsKey(client))
            {
                throw new IndexOutOfRangeException("这个玩家不在在线的字典里");
            }

            return clientIdDict[client];
        }

        /// <summary>
        /// 根据用户id获取数据模型
        /// </summary>
        /// <returns></returns>
        public UserModel GetModelById(int userId)
        {
            UserModel user = idModelDict[userId];
            return user;
        }

        /// <summary>
        /// 跟新用户信息
        /// </summary>
        /// <param name="model"></param>
        public void UpdateUserInfo(UserModel model)
        {
            idModelDict[model.Id] = model;
        }
    }
}